Fix concurrent Reflection.Emit TypeLoadException (#129230)#130696
Fix concurrent Reflection.Emit TypeLoadException (#129230)#130696steveisok wants to merge 1 commit into
Conversation
PR dotnet#125536 added an AddPropertyToLookUpTable call inside CMiniMdRW::AddPropertyToPropertyMap (and AddEventToLookUpTable inside AddEventToEventMap) so that the ENC/hot-reload path, which reaches those functions via metamodelenc.cpp rather than RegMeta::DefineProperty, would also maintain the property/event parent lookup table (fixing dotnet#125534). However the emit path already appended to the lookup table: DefineProperty and _DefineEvent call AddPropertyToPropertyMap/AddEventToEventMap and then also call Add*ToLookUpTable themselves, gated on HasIndirectTable. The S_FALSE branch in AddPropertyToPropertyMap fires under exactly the same condition (an indirect PropertyPtr/EventPtr table exists), so after dotnet#125536 the emit path appended the same <member, typedef> entry twice. Add*ToLookUpTable only appends once the lazily-built lookup map is non-NULL (the map is built on demand by a reader in FindParentOf*Helper). When a concurrent reader has built the map, the duplicate append desyncs the map's Count from the member RID, so a subsequently emitted member is recorded against the wrong parent typedef, surfacing as a TypeLoadException. This is why the regression only reproduces under concurrent emit + reflection. Remove the now-redundant emit-side appends so the lookup table is maintained in exactly one place (AddPropertyToPropertyMap/AddEventToEventMap), which serves both the emit and ENC/hot-reload callers and preserves the dotnet#125534 fix. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @agocke |
There was a problem hiding this comment.
Pull request overview
This PR adjusts CoreCLR’s metadata emit path to avoid duplicating updates to the property/event parent lookup tables, ensuring those tables are maintained in a single location (via CMiniMdRW::AddPropertyToPropertyMap / AddEventToEventMap). This is intended to prevent lookup-table corruption that can surface as TypeLoadException in concurrent Reflection.Emit + reflection scenarios.
Changes:
- Remove the emit-side
AddEventToLookUpTableappend fromRegMeta::_DefineEvent. - Remove the emit-side
AddPropertyToLookUpTableappend fromRegMeta::DefineProperty.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/coreclr/md/compiler/regmeta_emit.cpp | Removes redundant event lookup-table append in _DefineEvent. |
| src/coreclr/md/compiler/emit.cpp | Removes redundant property lookup-table append in DefineProperty. |
| IfFailGo(_SetPropertyProps(*pmdProp, dwPropFlags, dwCPlusTypeFlag, pValue, cchValue, mdSetter, | ||
| mdGetter, rmdOtherMethods)); |
|
Workflow state for the Holistic Review Orchestrator. {
"version": 5,
"last_dispatched_commit": "f52617bdb42de7edfbed8d70148e73b42553982c",
"last_dispatched_base_ref": "main",
"last_dispatched_base_sha": "081d9a410022f5edc3e11c0e8233d48b5e10f1bc",
"last_reviewed_commit": "f52617bdb42de7edfbed8d70148e73b42553982c",
"last_reviewed_base_ref": "main",
"last_reviewed_base_sha": "081d9a410022f5edc3e11c0e8233d48b5e10f1bc",
"last_recorded_worker_run_id": "29683996922",
"review_attempt_commit": "",
"review_attempt_base_ref": "",
"review_attempt_count": 0,
"max_review_attempts": 5,
"review_history_format": "holistic-review-disclosure-v1",
"review_history": [
{
"commit": "f52617bdb42de7edfbed8d70148e73b42553982c",
"review_id": 4730631666
}
]
} |
There was a problem hiding this comment.
Holistic Review
Motivation: PR #125536 added lookup-table maintenance inside CMiniMdRW::AddPropertyToPropertyMap / AddEventToEventMap so the ENC/hot-reload path (which reaches those functions via metamodelenc.cpp rather than RegMeta::DefineProperty) would also maintain the property/event parent lookup table (#125534). But the emit path already appended to the lookup table itself in DefineProperty / _DefineEvent, so after #125536 the emit path appended the same <member, typedef> entry twice. When a concurrent reader had lazily built the lookup map, the duplicate append desynced the map Count from the member RID, recording a subsequently emitted member against the wrong parent typedef and surfacing as a TypeLoadException (#129230). This is why the regression only reproduces under concurrent emit + reflection.
Approach: Remove the now-redundant emit-side AddPropertyToLookUpTable / AddEventToLookUpTable calls in DefineProperty and _DefineEvent, leaving lookup-table maintenance in exactly one place. I verified the S_FALSE (indirect-table-present) branch of AddPropertyToPropertyMap/AddEventToEventMap fires under the same HasIndirectTable(TBL_Property/TBL_Event) condition that the removed emit code checked, and that both the emit path (emit.cpp / regmeta_emit.cpp) and the ENC path (metamodelenc.cpp eDeltaPropertyCreate/eDeltaEventCreate) route through those map functions. The append is therefore still performed exactly once per member on both callers, and AddPropertyToLookUpTable/AddEventToLookUpTable remain no-ops until a reader lazily builds the map, so the #125534 fix is preserved. The _ASSERTE(RidFromToken(...) == Count()) invariants in the lookup functions now hold on the emit path, whereas the double-append previously violated them.
Summary: LGTM. This is a correct, minimal, and well-targeted fix that removes a genuine double-maintenance bug introduced by #125536 while retaining the behavior that #125536 needed for the ENC path. The change is symmetric across properties and events and touches only the two redundant blocks. Non-blocking observation: because the crash only reproduces under concurrent emit + reflection map building, no automated regression test is included; that is an understandable limitation given the timing sensitivity, but a stress-style test exercising concurrent DefineProperty/DefineEvent against reflection lookups would guard against future regressions if one can be made reliable.
Note
This review was generated by this repository's Holistic Review agentic workflow to complement the built-in Copilot review.
Generated by Holistic Review · 50.6 AIC · ⌖ 10.5 AIC · ⊞ 10K
PR #125536 added an AddPropertyToLookUpTable call inside CMiniMdRW::AddPropertyToPropertyMap (and AddEventToLookUpTable inside AddEventToEventMap) so that the ENC/hot-reload path, which reaches those functions via metamodelenc.cpp rather than RegMeta::DefineProperty, would also maintain the property/event parent lookup table (fixing #125534).
However the emit path already appended to the lookup table: DefineProperty and _DefineEvent call AddPropertyToPropertyMap/AddEventToEventMap and then also call Add*ToLookUpTable themselves, gated on HasIndirectTable. The S_FALSE branch in AddPropertyToPropertyMap fires under exactly the same condition (an indirect PropertyPtr/EventPtr table exists), so after #125536 the emit path appended the same <member, typedef> entry twice.
AddToLookUpTable only appends once the lazily-built lookup map is non-NULL (the map is built on demand by a reader in FindParentOfHelper). When a concurrent reader has built the map, the duplicate append desyncs the map's Count from the member RID, so a subsequently emitted member is recorded against the wrong parent typedef, surfacing as a TypeLoadException. This is why the regression only reproduces under concurrent emit + reflection.
Remove the now-redundant emit-side appends so the lookup table is maintained in exactly one place (AddPropertyToPropertyMap/AddEventToEventMap), which serves both the emit and ENC/hot-reload callers and preserves the #125534 fix.
Fixes #129230